package com.sky.controller.admin;/*
 * ClassName: SetMealController
 * Package : com.sky.controller.admin
 * Description:
 * @Author zKer
 * @Create 2024/4/14 22:19
 */

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetMealController {

    @Autowired
    private SetMealService setMealService;

    @GetMapping("{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("根据id查询套餐:{}",id);
        Result<SetmealVO> result = setMealService.getById(id);
        return result;
    }

    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(value = "setmealCatch",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.info("套餐新增:{}",setmealDTO);
        setMealService.save(setmealDTO);
        return Result.success();
    }

    @GetMapping("page")
    @ApiOperation("分页查询套餐")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询套餐:{}",setmealPageQueryDTO);
        PageResult pageResult = setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(value = "setmealCatch",allEntries = true)
    public Result delete(@RequestParam List<Long>ids){
        log.info("批量删除套餐:{}",ids);
        setMealService.delete(ids);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(value = "setmealCatch",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐:{}",setmealDTO);
        setMealService.update(setmealDTO);
        return Result.success();
    }

    @PostMapping("status/{status}")
    @ApiOperation("修改套餐起售停售状态")
    @CacheEvict(value = "setmealCatch",allEntries = true)
    public Result StartOrStop(@PathVariable Integer status,Long id){
        log.info("修改套餐起售停售状态:{},{}",status,id);
        setMealService.startOrStop(status,id);
        return Result.success();
    }
}
